﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VkontakteAgent
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private DataTable dt = null;
        UserAPI u = null;
        public MainForm(UserAPI ua)
        {
            this.u = ua;
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string s = string.Empty;
            Friend[] friends = u.UserFriends(1, 1000);
            dt = new DataTable();
            DataColumn column = new DataColumn("id");
            column.DataType = System.Type.GetType("System.Int32");
            column.ReadOnly = true;
            column.Unique = true;
            dt.Columns.Add(column);
            DataColumn column1 = new DataColumn("name");
            column1.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(column1);
            DataColumn column2 = new DataColumn("picurl");
            column2.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(column2);
            DataColumn column3 = new DataColumn("online");
            column3.DataType = System.Type.GetType("System.Boolean");
            dt.Columns.Add(column3);
            DataColumn column4 = new DataColumn("statustext");
            column3.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(column4);

            foreach (Friend f in friends)
            {
                /*s = u.ReadStatus(f.Id).StatusText;
                Console.WriteLine("UserID: " + f.Id
                    + " UserName: " + f.Name
                    + " UserStatus: " + s);
                */
                DataRow row = dt.NewRow();
                row["id"] = f.Id;
                row["name"] = f.Name;
                row["picurl"] = f.PicUrl;
                row["online"] = f.Online;
                row["statustext"] = u.ReadStatus(f.Id).StatusText;
                dt.Rows.Add(row);
            }
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "name";
            listBox1.ValueMember = "id";
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.Show();
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            String uid = listBox1.SelectedValue.ToString();
            DataRow[] rows = dt.Select("id='"+uid+"'");
            try
            {
                pictureBox1.Image = Image.FromStream(System.Net.HttpWebRequest.Create(rows[0]["picurl"].ToString()).GetResponse().GetResponseStream());
            }catch(Exception exc){
                pictureBox1.Image = null;
                MessageBox.Show("Невозможно открыть аватар!");
            }
            label1.Text = uid;
            label2.Text = rows[0]["name"].ToString();
            label3.Width = 200;
            label3.Text = rows[0]["statustext"].ToString();
            //MessageBox.Show(rows[0]["name"].ToString());
        }

    }
}
